package model

import (
	"github.com/jmoiron/sqlx"
	"time"
	"zimuzu_cvn_web_api/config"
	"zimuzu_cvn_web_api/pkg/e"
	"zimuzu_cvn_web_api/pkg/errno"
)

type CvnLog struct {
	ID             int64   `db:"id" json:"id,omitempty"`
	UID            int64   `db:"uid" json:"uid,omitempty"`
	Source         int     `db:"source" json:"source,omitempty"`
	LxCvnStatus    int     `db:"lx_cvn_status" json:"lx_cvn_status,omitempty"`
	LxCvnTime      string  `db:"lx_cvn_time" json:"lx_cvn_time,omitempty"`
	Cvn            float64 `db:"cvn" json:"cvn,omitempty"`
	TotalGainCvn   float64 `db:"total_gain_cvn" json:"total_gain_cvn,omitempty"`
	UnWithdrawnCvn float64 `db:"unwithdrawn_cvn" json:"unwithdrawn_cvn,omitempty"`
}

type CvnPledge struct {
	Cvn float64 `form:"cvn" json:"cvn"`
}

type StatusModel struct {
	Status int `form:"status" json:"status"`
}

type NodeChange struct {
	CvnPledge
	StatusModel
}

type DrawTaskModel struct {
	TaskId     int64   `db:"dl_id" json:"dl_id"`
	UID        int64   `db:"uid" json:"uid"`
	Cvn        float64 `db:"cvn" json:"cvn"`
	Time       int64   `db:"time" json:"time"`
	Status     int     `db:"status" json:"status"`
	RelationId int64   `db:"relation_id" json:"relation_id"`
}

const PledgeRouter = 2     // 路由器质押
const PledgeLinux = 1      // linux质押
const PledgePC = 3         // pc质押
const PledgeToken = 4      // 令牌质押
const PledgeDeposit = 5    // 保证金质押
const PledgeSupernode = 10 // 超级结点质押

// 插入提取任务表
// Auth: Hzp
func AddDrawTask(db sqlx.Execer, uid, timeInt, relationId int64, cvn float64, pledgeType int) (int64, error) {
	nowTime := time.Now().Unix()
	re := sqlx.MustExec(db, `INSERT INTO cvn_user_draw_task(uid, cvn, time, status, type ,create_time, relation_id) values (?, ?, ?, 0, ?, ?, ?)`,
		uid, cvn, timeInt, pledgeType, nowTime, relationId)
	reply, err := re.LastInsertId()
	if err != nil {
		e.Log("err", "插入提取任务表:", err.Error())
		return 0, err
	}
	return reply, nil
}

type FixedUnDoTaskModel struct {
	TaskId int64   `db:"dl_id" json:"dl_id"`
	Type   int     `db:"type" json:"type"`
	Time   int64   `db:"time" json:"time"`
	Status int     `db:"status" json:"status"`
	Uid    int64   `db:"uid" json:"uid"`
	Cvn    float64 `db:"cvn" json:"cvn"`
}

// 查未处理的提取任务添加到队列
func FixedUnDoTask() (re []FixedUnDoTaskModel, err error) {
	err = sqlx.Select(config.C.DB.DB, &re, "SELECT dl_id, uid,cvn,uid, type, status, time FROM cvn_user_draw_task where `status` = 0")
	if err != nil {
		return re, err
	}
	return re, nil
}

// 删除提取任务表
// Auth: Hzp
func RemoveDrawTask(db sqlx.Execer, TaskId int64) (err error) {
	re := sqlx.MustExec(db, `DELETE FROM cvn_user_draw_task WHERE dl_id = ?`, TaskId)
	_, err = re.RowsAffected()
	if err != nil {
		e.Log("err", "删除提取任务表:", err.Error())
		return err
	}
	return nil
}

// 更新提取任务表状态为已处理
// Auth: Hzp
func SetDoneDrawTask(db sqlx.Execer, TaskId int64) (err error) {
	re := sqlx.MustExec(db, "UPDATE cvn_user_draw_task SET `status` = 1 WHERE dl_id = ?", TaskId)
	_, err = re.RowsAffected()
	if err != nil {
		e.Log("err", " 更新提取任务表状态为已处理失败:", err.Error())
		return err
	}
	return nil
}

// 获取提取列表任务
// Auth: Hzp
func GetUserDrawTask(TaskId int64) (task DrawTaskModel, err error) {

	var count int
	err = sqlx.Get(config.C.DB.DB, &count, "SELECT count(*) FROM cvn_user_draw_task WHERE dl_id = ? and `status` = 0", TaskId)
	if count <= 0 {
		return task, errno.RecordError
	}

	err = sqlx.Get(config.C.DB.DB, &task, "SELECT dl_id,uid,cvn,time,status,relation_id From cvn_user_draw_task WHERE dl_id = ? and `status` = 0", TaskId)
	if err != nil {
		e.Log("err", "获取提取列表任务:", err.Error())
		return task, err
	}
	return task, nil
}
